// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.fowo.api.controller.shipment.entity.fbaInvoiceCancel;
import com.fowo.api.inventory.model.form.InventoryExchangeFormVo;
import com.fowo.api.inventory.model.form.InventoryOutboundFormVo;
import com.fowo.api.inventory.service.impl.InventoryBusinessServiceImpl;
import com.fowo.api.model.nav.sell.order.NavSellOrderVo;
import com.fowo.api.model.wms.order.WmsOrderVo;
import com.fowo.api.model.wms.order.item.WmsOrderItemItemVo;
import com.fowo.api.syncAmazon.api.FbaOutboundApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 销售出库单(wms) 服务实现类
 */
@Slf4j
@Service
public class WmsOrderServiceImpl extends WmsOrderBaseServiceImpl {

    @Resource
    private NavSellOrderServiceImpl sellOrderService;
    @Resource
    private FbaOutboundApiService fbaOutboundApiService;

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void cancesendgoods(fbaInvoiceCancel model) throws Exception {
        WmsOrderVo voById = this.getVoById(model.getId());
        Assert.notNull(voById, "订单不存在!");
        NavSellOrderVo item = sellOrderService.getVoById(voById.getParentNavSellOrder());
        item.setRemark(model.getRemarks());
        Assert.isTrue(voById.getStatus().equals("3"), "校验异常,请检查单据状态!");
        //1:撤销回待审核:撤回待审核状态会回退库存，订单需要重新审核下单  2:撤销回待发货
        if (model.getType().intValue() == 1) {
            //01 还回库存
            if (StrUtil.equals("1", item.getDeliveryWidType()) /*&& !this.hasTagHandler(item.getTag(), "28")*/) { //1 - 本地仓; 2 - 海外仓; 3 - FBA
                //撤销库存
                this.cancelWarehousing(voById.getId());

            } else if (StrUtil.equals("2", item.getDeliveryWidType())) { //1 - 本地仓; 2 - 海外仓; 3 - FBA
                //通知物流截单
                fbaOutboundApiService.cancelFulfillmentOrder(item.getOrderId(), item.getDeliveryWarehouse());
            }
            item.setCutStatus("1"); //1:截单中 2:异常
            item.setIssuStatus("0"); //标发状态
            item.setIssuStatusExplain("");
            item.setLogisticsOrderStatus("0");//物流下单状态
            item.setErrorMessage("");
            //删除销售出库单
            sellOrderService.deleteWmsOrder(CollUtil.newArrayList(item));
            sellOrderService.changeNavSellOrderStatus(CollUtil.newArrayList(item), "1");
        }
        //2撤销待到货:撤回待发货状态不会回退库存，支持重新打单拣货，验货等   回退到锁定的库存
        else if (model.getType().intValue() == 2) {
            //01 还回库存
            if (StrUtil.equals("1", item.getDeliveryWidType()) /*&& !this.hasTagHandler(item.getTag(), "28")*/) { //1 - 本地仓; 2 - 海外仓; 3 - FBA
                //撤销到锁定
                this.cancelpWarehousing(voById.getId());
            }
            sellOrderService.changeWmsOrderStatus(CollUtil.newArrayList(item), "2");
            sellOrderService.changeNavSellOrderStatus(CollUtil.newArrayList(item), "3");
        } else {
            Assert.isTrue(false, "撤销类型错误");
        }
    }


    /**
     * 准备用于 订单（从锁定库存出库） 的参数
     */
    protected InventoryOutboundFormVo prepareLockStockOut(WmsOrderVo vo) throws Exception {
        for (WmsOrderItemItemVo itemVo : vo.getProductInfo()) {
            if (ObjUtil.isEmpty(itemVo.getWhbId())) {
                Long id = SpringUtil.getBean(InventoryBusinessServiceImpl.class).getSearchByWarehouseBin(vo.getWid(), null, null, itemVo.getProductId(), 1);
                itemVo.setWhbId(id);
            }
        }
        super.update(vo);
        return super.prepareLockStockOut(vo);
    }

    /**
     * 准备用于 订单锁库存（锁库存_即时） 的参数
     */
    protected InventoryExchangeFormVo prepareLocksInventory(WmsOrderVo vo) throws Exception {
        for (WmsOrderItemItemVo itemVo : vo.getProductInfo()) {
            if (ObjUtil.isEmpty(itemVo.getWhbId())) {
                Long id = SpringUtil.getBean(InventoryBusinessServiceImpl.class).getSearchByWarehouseBin(vo.getWid(), null, null, itemVo.getProductId(), 1);
                itemVo.setWhbId(id);
            }
        }
        super.update(vo);
        return super.prepareLocksInventory(vo);
    }

    /**
     * 准备用于 订单锁库存（锁库存_仓位） 的参数
     */
    protected InventoryExchangeFormVo prepareLockpInventory(WmsOrderVo vo) throws Exception {
        for (WmsOrderItemItemVo itemVo : vo.getProductInfo()) {
            if (ObjUtil.isEmpty(itemVo.getWhbId())) {
                Long id = SpringUtil.getBean(InventoryBusinessServiceImpl.class).getSearchByWarehouseBin(vo.getWid(), null, null, itemVo.getProductId(), 1);
                itemVo.setWhbId(id);
            }
        }
        super.update(vo);
        return super.prepareLockpInventory(vo);
    }

    /**
     * 准备用于 扣库存 的参数
     */
    protected InventoryOutboundFormVo prepareSubInventory(WmsOrderVo vo) throws Exception {
        for (WmsOrderItemItemVo itemVo : vo.getProductInfo()) {
            if (ObjUtil.isEmpty(itemVo.getWhbId())) {
                Long id = SpringUtil.getBean(InventoryBusinessServiceImpl.class).getSearchByWarehouseBin(vo.getWid(), null, null, itemVo.getProductId(), 1);
                itemVo.setWhbId(id);
            }
        }
        super.update(vo);
        return super.prepareSubInventory(vo);
    }


    /**
     * 准备用于 销售出库 的参数
     */
    protected InventoryOutboundFormVo prepareSalesOutWarehouse(WmsOrderVo vo) throws Exception {
        for (WmsOrderItemItemVo itemVo : vo.getProductInfo()) {
            if (ObjUtil.isEmpty(itemVo.getWhbId())) {
                Long id = SpringUtil.getBean(InventoryBusinessServiceImpl.class).getSearchByWarehouseBin(vo.getWid(), null, null, itemVo.getProductId(), 1);
                itemVo.setWhbId(id);
            }
        }
        super.update(vo);
        return super.prepareSalesOutWarehouse(vo);
    }

    /**
     * 准备用于 直接扣减库存 的参数
     */
    protected InventoryOutboundFormVo prepareStockOut(WmsOrderVo vo) throws Exception {
        for (WmsOrderItemItemVo itemVo : vo.getProductInfo()) {
            if (ObjUtil.isEmpty(itemVo.getWhbId())) {
                Long id = SpringUtil.getBean(InventoryBusinessServiceImpl.class).getSearchByWarehouseBin(vo.getWid(), null, null, itemVo.getProductId(), 1);
                itemVo.setWhbId(id);
            }
        }
        super.update(vo);
        return super.prepareStockOut(vo);
    }

}
